package com.zsule.controllers.manager;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.zsule.controllers.BaseController;
import com.zsule.entity.PageEntity;
import com.zsule.entity.manager.Manager;
import com.zsule.service.manager.IManagerService;
import com.zsule.utils.security.MD5;

@Controller
@RequestMapping("/manager/muser")
public class ManagerController extends BaseController {

	private static final Logger logger = Logger.getLogger(ManagerController.class);

	@Autowired
	private IManagerService managerService;

	// 路径
	private String toList = "/manager/muser/user_list.httl";// 产品表页
	private String toAdd = "/manager/muser/user_add.httl";// 添加页面
	private String toEdit = "/manager/muser/user_edit.httl";// 修改页

	@RequestMapping("/list")
	public ModelAndView listAll(HttpServletRequest request, HttpServletResponse response, Manager query, @ModelAttribute("page") PageEntity page) {
		ModelAndView modelAndView = new ModelAndView(toList);
		try {
			this.setPage(page);
			this.getPage().setPageSize(20);
			if (query == null) {
				query = new Manager();
			}
			List<Manager> list = managerService.getManagerPage(query, this.getPage());
			modelAndView.addObject("query", query);
			modelAndView.addObject("managerList", list);
			modelAndView.addObject("page", this.getPage());
		} catch (Exception e) {
			logger.error("ManagerController.listAll", e);
		}

		return modelAndView;
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView toAdd() {
		ModelAndView modelAndView = new ModelAndView(toAdd);
		try {
		} catch (Exception e) {
			logger.error("ManagerController.toAdd", e);
		}
		return modelAndView;
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public RedirectView add(Manager manager, HttpServletRequest request) {
		try {
			manager.setLoginPwd(MD5.getMD5(manager.getLoginPwd()));
			managerService.save(manager);
		} catch (Exception e) {
			logger.error("ManagerController.add", e);
		}
		return new RedirectView("/manager/muser/list");
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView toEdit(String id) {
		ModelAndView modelAndView = new ModelAndView(toEdit);
		try {
			Manager manager = managerService.findById(id);
			modelAndView.addObject("user",manager);
		} catch (Exception e) {
			logger.error("ManagerController.toEdit", e);
		}
		return modelAndView;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public RedirectView edit(Manager manager, HttpServletRequest request) {
		try {
			Manager old = managerService.findById(manager.getId());
			if(!old.getLoginPwd().equals(manager.getLoginPwd())){
				manager.setLoginPwd(MD5.getMD5(manager.getLoginPwd()));
			}
			managerService.save(manager);
		} catch (Exception e) {
			logger.error("ManagerController.edit", e);
		}
		return new RedirectView("/manager/muser/list");
	}

	@RequestMapping("/delete")
	public RedirectView delete(String ids, HttpServletRequest request, Manager query, @ModelAttribute("page") PageEntity page, RedirectAttributes attr) {
		RedirectView rv = new RedirectView("/manager/muser/list");
		String[] idArray = ids.split(",");
		try {// 软删除状态设置为2
			for (String id : idArray) {
				if (!"".equals(id)) {
					this.managerService.removeById(id);
				}
			}
		} catch (Exception e) {
			logger.error("ManagerController.delete", e);
		}
		return rv;
	}

}
